package com.ruoyi.api.service.impl;


import com.ruoyi.api.entity.Location;
import com.ruoyi.api.service.LocationService;
import com.ruoyi.api.vo.Approval;
import com.ruoyi.api.vo.ApprovalDemo;
import com.ruoyi.api.vo.Locations;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.vim.mapper.LocationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static org.apache.commons.lang3.ObjectUtils.isEmpty;

/**
 *路径和坐标信息
 */
@Service
public class LocationServiceImpl implements LocationService {


    @Autowired
    private LocationMapper locationMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 添加路径和坐标
     * @param locations
     */
    @Override
    public void add(Locations locations) {
        Date date = new Date();
        DateFormat dateFormats = new SimpleDateFormat("yyyyMMddHHmmss");
        String times = dateFormats.format(date);
        locations.setTime(date);
        long timestamp = date.getTime();
        //将时间存储为时间戳
        locations.setUseTime(timestamp);
        //获取上传信息的月份(数据库使用动态sql,按月份创建表分区)
        String userId = locations.getUserId();
        String track_id = locations.getUserId() + "_" + times;
        Object a = redisCache.getCacheObject(userId);
        String location = locations.getLocation();
        //判断key值失效时,将此时的userId,trackId作为key,value进行存储,并设置固定时间key值过期
        if(isEmpty(a)){
            locations.setTrackId(track_id);
            redisCache.setCacheObject("userId:" + userId,track_id,5, TimeUnit.MINUTES);
            Boolean b = locationMapper.addlocation(locations);
            redisCache.setCacheObject("userLocation:" + userId + "location",location);
        }
        //判断同一用户的key值是否失效,判断未失效延用上一个redis中的value值作为路径值trackId,并保存到数据库中
        else {
            Object b = redisCache.getCacheObject("userLocation:" + userId + "location");
            //判断同一路径下用户位置是否发生改变,改变时使用新的位置坐标,未改变只修改时间
            if(location.equals(String.valueOf(b))){
                Object id = redisCache.getCacheObject("userId:" + userId);
                locations.setTrackId(String.valueOf(id));
                DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
                String tableName = "im_location_" + dateFormat.format(date);
                locationMapper.updatelocation(locations,tableName);
            }else {
                Object id = redisCache.getCacheObject("userId:" + userId);
                locations.setTrackId(String.valueOf(id));
                Boolean c = locationMapper.addlocation(locations);
                redisCache.setCacheObject("userLocation:" + userId + "location",location);
            }
        }
    }

    /**
     * 查询用户路径信息
     * @param userId
     * @return
     */
    @Override
    public List<String> selectPath(String userId, Date time) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        String tableName = "im_location_" + dateFormat.format(date);
        List<String> list = locationMapper.selectPath(userId,tableName,time);
        return list;
    }

    /**
     * 查询用户路径上位置信息
     * @param trackId
     * @return
     */
    @Override
    public List<Locations> selectlocation(String trackId) {
        String a = trackId.split("_")[1];
        String b = a.substring(0,6);
        String tableName = "im_location_" + b;
        List<Locations> list = locationMapper.selectlocation(trackId,tableName);
        return list;
    }

    /**
     * 根据路径Id删除 用户的路径信息
     * @param trackId
     * @return
     */
    @Override
    public boolean deletepath(String trackId) {
        boolean a = locationMapper.deleteBytrackId(trackId);
        return a;
    }

    @Override
    public String selectUserLocation(String userId) {
        String location = locationMapper.selectUserLocation(userId);
        return location;
    }

    @Override
    public void addOffline(List<Locations> list) {
        for (Locations locations : list) {
            long timestamp = locations.getTime().getTime();
            locations.setUseTime(timestamp);
            locationMapper.addlocation(locations);
        }
    }

    @Override
    public List<ApprovalDemo> selectByCar(Approval approval) {
        String a = String.valueOf(approval.getUserId());
        Long b = approval.getBeginTime();
        Long c = approval.getEndTime();
//        String carInfo = approval.getCarInfo();
        List<ApprovalDemo> demoList = new ArrayList<>();
//        if(a.equals("1")){
//            Set<String> set = locationMapper.selectByCars(carInfo,b,c);
//            for (String s : set) {
//                ApprovalDemo approvalDemo = new ApprovalDemo();
//                List<Locations> list = selectlocation(s);
//                approvalDemo.setBeginTime(list.get(0).getTime());
//                approvalDemo.setEndTime(list.get(list.size()-1).getTime());
//                approvalDemo.setTraceId(s);
//                approvalDemo.setUserId(list.get(0).getUserId());
//                demoList.add(approvalDemo);
//            }
//            return demoList;
//        }else
            Set<String> set = locationMapper.selectByCar(a,b,c);
            for (String s : set) {
                ApprovalDemo approvalDemo = new ApprovalDemo();
                List<Locations> list = selectlocation(s);
                approvalDemo.setBeginTime(list.get(0).getTime());
                approvalDemo.setEndTime(list.get(list.size()-1).getTime());
                approvalDemo.setTraceId(s);
                approvalDemo.setUserId(list.get(0).getUserId());
                demoList.add(approvalDemo);
            }
            return demoList;
    }

    @Override
    public List<String> selectByTrack(String trackId) {
        return locationMapper.selectTrack(trackId);
    }

}




